<?php
/**
 * Created by PhpStorm.
 * User: cc
 * Date: 2021/4/15
 * Time: 2:12 下午
 */

namespace app\api\controller\v25;

use app\api\controller\v2\Auth;
use app\common\cachemodel\Goods;
use app\common\cachemodel\NatalGoods;
use app\common\cachemodel\NatalReport;
use app\common\cachemodel\Orders;
use app\common\cachemodel\ZhunleActivity;
use app\common\cachemodel\ZlSynastryReport;
use app\common\lib\ConfigManage;
use app\common\model\Recode;

/**
 * 鉴缘分报告列表
 * @version 2.6.0
 * Class LoveNatal_Report
 * @package app\api\controller\v25
 */
class LoveNatalReport extends Auth
{
    protected $gid = [39, 40];//需要展示的本命报告商品id

    public function index()
    {
        $rid = input('rid/d', 0);
        if (!$rid)
        {
            return show('code.success', '请求异常');
        }
        $orderModel = new Orders();
        $NatalReportModel = new NatalReport();
        $activingModel = new ZhunleActivity();
        $recodeModel = new Recode();
        $recodeInfo = $recodeModel->getRecode(['uid' => $this->user['id'], 'id' => $rid]);
        //获取报告商品图片
        $reportImage = ConfigManage::natalReportGoodImage(1);
        //  获取本命预测商品信息
        $natalGoodData = (new NatalGoods())->getGoods();
        // 获取合盘报告商品
        $synastryReportGoods = (new Goods())->GoodsTypeInfo(17);
        $goodData = array_merge($synastryReportGoods, $natalGoodData);
        $forecast = [];
        // 获取解锁订单信息状态
        $unlockReport = $NatalReportModel->getNatalReport2($recodeInfo);
        foreach (array_values($goodData) as $key => $item)
        {
            $goodType = (isset($item['good_type']) && $item['good_type'] === 17) ? 17 : 7;
            if ((int)$item['status'] === 2)//暂不开放状态
            {
                $status = 3;
            } else//判断商品解锁状态
            {
                if ($goodType === 17)
                {   // 合盘报告
                    // 检测 是否购买过
                    $unlockStatus = (new ZlSynastryReport())->getUserHasReportUnlockStatus($this->user['id'], $rid, 0, $item['srid']);
                    $status = $unlockStatus ? 2 : 0;
                } else
                {   // 本命报告
                    if (isset($unlockReport[$item['id']]))
                    {
                        $unlock_status = (int)$unlockReport[$item['id']]['status'] === 2 ? 2 : 1;
                        $status = isset($unlockReport[$item['id']]) ? ($unlock_status) : 0;
                    } else
                    {
                        $status = 0;
                    }
                }
            }

            if ($goodType === 17)
            {  // 合盘报告
                $bg_img = $item['is_large'] ? $item['bg_img'] : $item['small_bg_img'];
            } else
            {   // 本命报告
                //获取活动商品信息
                $activing = $activingModel->getActivingGood($goodType, $item['id']);
                $actiGids = $activing ? explode(',', $activing['gids']) : [];

                //获取当前长图
                $bg_img = $reportImage['large'][$item['flag']];

                if (in_array($item['id'], $actiGids, false))
                {
                    $bg_img = $reportImage['large_active'][$item['flag']];
                }
            }

            if ($goodType === 17)
            {
                $d = [
                    'status' => $status,
                    'bg_img' => $bg_img,
                    'unlock_num' => $item['base_num'] + $orderModel->getTotalPrepaidOrder($goodType, $item['srid']),
                    'gcolor' => $item['color'] ?? '',
                    'gname' => $item['name'] ?? '',
                    'active_time' => 0,
                    'target' => 'local',
                    'link' => $status === 0 ?
                        "synastry_report_detail?rid1={$rid}&rid2=0&srid={$item['srid']}&need_auth=1" :    // 下单介绍页
                        "synastry_report_list?rid1={$rid}&rid2=0&srid={$item['srid']}&need_auth=1"  // 查看合盘报告列表
                ];
                $forecast[] = $d;
            } elseif (in_array($item['id'], $this->gid, false))
            {
                $d = [
                    'status' => $status,
                    'bg_img' => $bg_img,
                    'unlock_num' => $item['base_num'] + $orderModel->getTotalPrepaidOrder($goodType, $item['id']),
                    'gcolor' => $item['color'] ?? '',
                    'gname' => $NatalReportModel->gidName[$item['flag']] ?: '',
                    'active_time' => 0,
                    'target' => 'local',
                    'link' => "report_details?rid={$rid}&gid={$item['id']}&need_auth=1"
                ];
                $forecast[] = $d;
            }
        }
        return show(config('code.success'), '请求成功', $forecast);
    }
}
